package com.uphone.kingmall.adapter;

import android.app.Activity;
import android.view.View;
import android.widget.CheckBox;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.china.zhuoyue.R;
import com.uphone.kingmall.bean.ShoppingCartBean;

/**
 * <pre>
 *     author : radish
 *     e-mail : 15703379121@163.com
 *     time   : 2019/3/20
 *     desc   :
 * </pre>
 */
public class ShoppingCartAdapter extends BaseQuickAdapter<ShoppingCartBean.ShopCarBean, BaseViewHolder> {
    private final Activity activity;
    private OnShopItemClickListener listener;

    public ShoppingCartAdapter(Activity activity) {
        super(R.layout.kingmall_item_shopping_cart);
        this.activity = activity;
    }

    @Override
    protected void convert(final BaseViewHolder helper, final ShoppingCartBean.ShopCarBean item) {
        final CheckBox iv_select = helper.getView(R.id.iv_select);
        iv_select.setChecked(item.isSelect());
        helper.setText(R.id.tv_shop_name, item.getShopName() + "");
        RecyclerView rvGoods = helper.getView(R.id.rv_goods);
        rvGoods.setLayoutManager(new LinearLayoutManager(mContext));
        final ShoppingCartItemAdapter adapter = new ShoppingCartItemAdapter(activity);
        rvGoods.setAdapter(adapter);
        adapter.setNewData(item.getGoods());
        adapter.setOnGoodsItemClickListener(new ShoppingCartItemAdapter.OnGoodsItemClickListener() {
            @Override
            public void itemClick(View v, int childPosition, ShoppingCartBean.ShopCarBean.GoodsBean childItem) {
                if (listener != null) {
                    listener.itemGoodClick(v, childPosition, childItem);
                }
            }

            @Override
            public void itemSelect(View v, int childPosition, ShoppingCartBean.ShopCarBean.GoodsBean childItem) {
                item.getGoods().set(childPosition, childItem);
                if (!childItem.isSelect() && item.isSelect()) {
                    item.setSelect(!item.isSelect());
                    iv_select.setChecked(item.isSelect());
                } else {
                    boolean check = true;
                    for (int i = 0; i < item.getGoods().size(); i++) {
                        if (!item.getGoods().get(i).isSelect()) {
                            check = false;
                            break;
                        }
                    }
                    item.setSelect(check);
                    iv_select.setChecked(item.isSelect());
                }
                if (listener != null) {
                    listener.itemGoodSelect(v, helper.getLayoutPosition(), childPosition, item, childItem);
                }
            }

            @Override
            public void itemAmount(View v, int childPosition, ShoppingCartBean.ShopCarBean.GoodsBean childItem, int amount, int type) {
                item.getGoods().set(childPosition, childItem);
                if (listener != null) {
                    listener.itemGoodAmount(v, helper.getLayoutPosition(), childPosition, item, childItem, amount, type);
                }
            }
        });

        helper.getView(R.id.tv_shop_name).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.itemClick(v, helper.getLayoutPosition(), item);
                }
            }
        });
        iv_select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isChecked = iv_select.isChecked();
                item.setSelect(isChecked);
                for (int i = 0; i < item.getGoods().size(); i++) {
                    item.getGoods().get(i).setSelect(isChecked);
                    adapter.setNewData(item.getGoods());
                }
                if (listener != null) {
                    listener.itemSelect(iv_select, helper.getLayoutPosition(), item);
                }
            }
        });
    }

    public void setOnShopItemClickListener(OnShopItemClickListener listener) {
        this.listener = listener;
    }

    public interface OnShopItemClickListener {
        void itemClick(View v, int position, ShoppingCartBean.ShopCarBean item);

        void itemSelect(View v, int position, ShoppingCartBean.ShopCarBean item);

        void itemGoodClick(View v, int childPosition, ShoppingCartBean.ShopCarBean.GoodsBean childItem);

        void itemGoodSelect(View v, int position, int childPosition, ShoppingCartBean.ShopCarBean item, ShoppingCartBean.ShopCarBean.GoodsBean childItem);

        void itemGoodAmount(View v, int position, int childPosition, ShoppingCartBean.ShopCarBean item, ShoppingCartBean.ShopCarBean.GoodsBean childItem, int amount, int type);
    }

}
